package com.ty.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;
import com.ty.common.validator.group.SaveGroup;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.TruckTeamEntity;
import com.ty.skysystem.entity.req.TruckTeamEntityREQ;
import com.ty.skysystem.service.TruckTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 车队信息表：用于存放和管理车队信息
 *
 * @author tianyuzhu
 * @email 1659770218@QQ.COM
 * @date 2020-12-11 11:12:37
 */
@RestController
@RequestMapping("truckteam")
@Api(tags = "车队信息管理模块: 用于存放和管理车队信息")
@Slf4j
public class TruckTeamController {
  @Autowired private TruckTeamService truckTeamService;

  /** 列表 */
  @ApiOperation(value = "查询全部车队信息", notes = "查询全部车队信息", response = TruckTeamEntity.class)
  @GetMapping("/list")
  public R list(
      @ApiParam(name = "根据传递参数进行查询", value = "params") @RequestParam Map<String, Object> params) {
    PageUtils page = this.truckTeamService.queryPage(params);
    return R.ok().put(page);
  }

  /** 信息 */
  @ApiOperation(value = "查询单个车队信息", notes = "查询单个车队信息", response = TruckTeamEntity.class)
  @GetMapping("/info/{teamId}")
  public R info(
      @ApiParam(name = "根据ID单查", value = "teamId", required = true) @PathVariable("teamId")
          Long teamId) {
    QueryWrapper<TruckTeamEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper
        .select(TruckTeamEntity.class, e -> !"is_deleted".equals(e.getColumn()))
        .eq("team_id", teamId);
    TruckTeamEntity truckTeam = this.truckTeamService.getOne(queryWrapper);

    return R.ok().put(truckTeam);
  }

  /** 保存 */
  @ApiOperation(value = "保存车队信息", notes = "保存车队信息", response = R.class)
  @PostMapping("/save")
  public R save(
      @ApiParam(name = "传递车队信息实体类信息", value = "truckTeam", required = true)
          @Validated(value = {SaveGroup.class})
          @RequestBody
          TruckTeamEntity truckTeam) {
    return truckTeamService.save(truckTeam)
        ? R.ok(ResultMsgEnum.INSERT_OK.getMsg())
        : R.error(ResultMsgEnum.INSERT_ERROR.getMsg());
  }

  /** 修改 */
  @ApiOperation(value = "修改车队信息", notes = "修改车队信息", response = R.class)
  @PatchMapping("/update")
  public R update(
      @ApiParam(name = "传递车队信息实体类信息", value = "truckTeam", required = true)
          @Validated(value = {UpdateGroup.class})
          @RequestBody
          TruckTeamEntity truckTeam) {
    truckTeam.setUpdateTime(new Date());
    return truckTeamService.updateById(truckTeam)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 删除 */
  @ApiOperation(value = "删除车队信息(id数组, 逻辑删除)", notes = "删除车队信息(id数组, 逻辑删除)", response = R.class)
  @DeleteMapping("/delete")
  public R delete(
      @ApiParam(name = "传递需要删除的ID数组", value = "teamIds", required = true) @RequestBody
          Long[] teamIds) {
    if (teamIds.length == 0) {
      return R.error("ID不能为空");
    }
    return truckTeamService.removeByIds(Arrays.asList(teamIds))
        ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
        : R.error(ResultMsgEnum.DELETE_ERROR.getMsg());
  }

  /** 运力综合查询->查询承运车队 */
  @ApiOperation(value = "运力综合查询->查询承运车队", response = TruckTeamEntity.class)
  @GetMapping("/listFleetCapacity")
  public R fleetCapacity(
          @RequestParam(value = "page", defaultValue = "1") Integer page,
          @RequestParam(value = "limit", defaultValue = "5") Integer limit,
          TruckTeamEntityREQ truckTeamEntityREQ) {
    PageUtils pageUtils =
            truckTeamService.selectComprehensiveTonnage(page, limit, truckTeamEntityREQ);
    return R.ok().put(pageUtils);
  }
}
